/**
 * 传感器相关 API
 */
import http from '../utils/request'

export interface SensorData {
  id: number
  device_id: number
  timestamp: string
  data_type: string
  value: number
  unit?: string
  quality: number
  created_at: string
}

export interface SensorStatistics {
  device_id: number
  device_code: string
  device_name: string
  sensor_type: string
  time_range: {
    start_time: string
    end_time: string
  }
  data_count: number
  avg_value?: number
  min_value?: number
  max_value?: number
  latest_value?: number
  latest_timestamp?: string
  unit?: string
}

export interface SensorType {
  type: string
  name: string
  description: string
}

// 获取传感器数据
export const getSensorData = (params: {
  device_id: number
  sensor_type?: string
  start_time?: string
  end_time?: string
  page?: number
  page_size?: number
}) => {
  return http.get('/sensors/data', { params })
}

// 获取设备最新传感器数据
export const getLatestSensorData = (device_id: number) => {
  return http.get('/sensors/latest', { 
    params: { device_id }
  })
}

// 获取传感器数据统计
export const getSensorStatistics = (params: {
  device_id: number
  sensor_type: string
  start_time?: string
  end_time?: string
}) => {
  return http.get<SensorStatistics>('/sensors/statistics', { params })
}

// 获取传感器类型列表
export const getSensorTypes = () => {
  return http.get<SensorType[]>('/sensors/types')
}